package org.ecloud.base.swagger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StopWatch;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger.web.UiConfigurationBuilder;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SpringfoxSwaggerConfiguration extends WebMvcConfigurerAdapter {

	private static Logger logger = LoggerFactory.getLogger(SpringfoxSwaggerConfiguration.class);
	@Value("${spring.profiles.active}")
	private String profile;
	@Value("${swagger.info.title}")
	private String title;
	@Value("${swagger.info.description}")
	private String description;
	@Value("${swagger.info.base-package}")
	private String basePackage;
	@Value("${swagger.info.version}")
	private String version;
	
	@Bean
    public Docket createRestApi() {
		logger.debug("Starting Swagger");
		StopWatch watch = new StopWatch();
	    watch.start();
	    
	    ApiSelectorBuilder builder = new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select();
	    builder.apis(RequestHandlerSelectors.basePackage(basePackage));
	    builder.apis(RequestHandlerSelectors.withClassAnnotation(Api.class));
	    builder.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class));
	    if("dev".equalsIgnoreCase(profile)){
	    	builder.paths(PathSelectors.any());
	    }else{
	    	builder.paths(PathSelectors.none());
	    }
	    
	    Docket docket = builder.build();
	    watch.stop();
	    logger.debug("Started Swagger in {} ms", watch.getTotalTimeMillis());
	    return docket;
    }
	
	@Bean
    public UiConfiguration uiConfig() {
        return UiConfigurationBuilder.builder().validatorUrl("").build();
    }
	
	@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(new String[]{"swagger-ui.html"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/"});
        registry.addResourceHandler(new String[]{"/webjars*"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"});
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .version(version)
                .build();
    }
    
}
